package net.sourceforge.krama.js;

import net.sourceforge.krama.util.TUtil;

/**
 * 
 * @author chabane
 * 
 */
public class GenerateJS {

	private String jsCode = "";

	/**
	 * GPL License description
	 * @return String
	 */
	private String copyright(){
		
		return "/* Krama JavaScript framework, version 1.0 \n"+
			   " * (c) 2011-2012 REFES Chabane \n"+
			   " * \n"+
			   " * Krama is freely distributable under the terms of an GPL license. \n"+
			   " * For details, see the Krama Framework web site: http://www.kramaframework.org/ \n"+
			   " * \n"+
			   " *--------------------------------------------------------------------------*/  \n";
	}
	
	/**
	 * This method allows to call Ajax request and to fill a idDiv with an
	 * object returned by Ajax Servlet
	 * 
	 * @return String
	 */
	private String onClick() {
		return "function clic(cName, mName, paramsTab, idDiv) { \n" +

		"var url = 'result.aj?';" + "url += 'cName=' + cName;\n"
				+ "   url += '&mName=' + mName;\n"

				+ "  if (paramsTab.length > 0) {\n"
				+ "url += '&nParam=' + paramsTab.length;\n"

				+ "	for ( var i = 0; i < paramsTab.length; i++) {\n"
				+ "		url += '&param' + (i+1) + '=' + paramsTab[i];" + "	}\n"
				+ "} else {" + "	url += '&nParam=' + 0;" + "}\n"
				+ "new Ajax.Request(url, {\n" + "	method : 'get',\n"
				+ "	onSuccess : function(xhr) {\n"
				+ "$(idDiv).innerHTML = xhr.responseText; \n" + "}\n"
				+ "	});\n" + "}\n\n";
	}

	/**
	 * call addJavascript function for add javascript file
	 * 
	 * @param jsName
	 * @param pos
	 * @return String
	 */
	private String addJavascriptFile(String jsName, String pos) {
		return "addJavascript('" + jsName + ".js','" + pos + "'); \n\n";
	}

	/**
	 * Allows to add a javascript file in JSP file
	 * 
	 * @return String
	 */
	private String importJSFile() {

		return "function addJavascript(jsname,pos) { \n"
				+ "var th = document.getElementsByTagName(pos)[0];\n"
				+ "var s = document.createElement('script');\n"
				+ "s.setAttribute('type','text/javascript');\n"
				+ "s.setAttribute('src',jsname);\n" + "th.appendChild(s);\n"
				+ "} \n";
	}
	
	/**
	 * Allows to drag any Widget of Krama Framework
	 * 
	 * @return String
	 */
	private String drag(){
		
		return "function dragWidget(id) { \n"
				+ "Drag.init(document.getElementById(id));  \n"
				+ "} \n";
	}
	/**
	 * Call all fonction javascript
	 * 
	 * @return String
	 */
	public String getCode() {

		if (TUtil.javascriptShouldBeAvailable) {
			jsCode  = copyright();
			jsCode += importJSFile();

			if (TUtil.prototypeShouldBeAvailable) {
				jsCode += addJavascriptFile("js/prototype", "head");
				if (TUtil.clicShouldBeAvailable)
					jsCode += onClick();
			}
			if (TUtil.jQueryShouldBeAvailable)
				jsCode += addJavascriptFile("js/jquery", "head");
			if (TUtil.doJoShouldBeAvailable)
				jsCode += addJavascriptFile("js/dojo", "head");
			if (TUtil.domDragShouldBeAvailable){
				jsCode += addJavascriptFile("js/dom-drag", "head");
				jsCode += drag();
			}
		}
		return jsCode;
	}
}
